package org.colcon.web.client.ui.panel;

import org.colcon.web.client.Collective_Congress;
import org.colcon.web.client.ui.ColConPanel;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class RegisterPanel extends ColConPanel
{
	private VerticalPanel registerPanel = new VerticalPanel();
	
	private Grid grid = new Grid(5, 2);
	
	private Label emailLabel = new Label("Email:");
	private TextBox emailBox = new TextBox();
	private Label nameLabel = new Label("Username:");
	private TextBox nameBox = new TextBox();
	private Label passwordLabel = new Label("Password:");
	private PasswordTextBox passwordBox = new PasswordTextBox();
	private Label confirmLabel = new Label("Confirm:");
	private PasswordTextBox confirmBox = new PasswordTextBox();
	private Button button = new Button("Register");
	
	public RegisterPanel(Collective_Congress client)
	{
		super(client, null, null);
		
		setStyleName("register");
		
		registerPanel.setWidth("100%");
		registerPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		
		button.addClickHandler(new MyButtonHandler());
		
		sinkEvents(Event.ONKEYPRESS);
		addHandler(new MyEnterHandler(), KeyPressEvent.getType());
		
		grid.setWidget(0, 0, emailLabel);
		grid.setWidget(0, 1, emailBox);
		grid.setWidget(1, 0, nameLabel);
		grid.setWidget(1, 1, nameBox);
		grid.setWidget(2, 0, passwordLabel);
		grid.setWidget(2, 1, passwordBox);
		grid.setWidget(3, 0, confirmLabel);
		grid.setWidget(3, 1, confirmBox);
		grid.setWidget(4, 1, button);

		registerPanel.add(grid);
		add(registerPanel);
		
		setFocus(emailBox);
	}
	
	private class MyButtonHandler implements ClickHandler
	{
		public void onClick(ClickEvent event) 
		{
			buttonClick();
		}
	}
	
	private class MyEnterHandler implements KeyPressHandler
	{
		public void onKeyPress(KeyPressEvent event) 
		{
			if(event.getCharCode() == KeyCodes.KEY_ENTER)
				buttonClick();
		}
	}
	
	public void buttonClick() 
	{
		if(!validate())
		{
			registerPanel.clear();
			registerPanel.add(grid);
			registerPanel.add(new HTML("Invalid input"));
			return;
		}
		
		registerPanel.clear();
		registerPanel.add(new HTML("Loading..."));
		
		String cmd = "add_user -id " + nameBox.getText() + " -email " + emailBox.getText() + " -password " + passwordBox.getText();
		
		getService().execute(cmd, new AsyncCallback<String>()
		{
			public void onFailure(Throwable caught)
			{
				caught.printStackTrace();
				
				registerPanel.clear();
				registerPanel.add(grid);
				registerPanel.add(new HTML(caught.getMessage()));
			}
			
			public void onSuccess(String result)
			{
				registerPanel.clear();
				registerPanel.add(new Label(result));
			}
		});
	}
	
	private boolean validate()
	{
		return
			!(
				emailBox.getText().isEmpty() ||	
				nameBox.getText().isEmpty() ||	
				passwordBox.getText().isEmpty() ||	
				confirmBox.getText().isEmpty()
			) &&
			passwordBox.getText().equals(confirmBox.getText());
	}
}
